package org.dromara.mall.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import net.bytebuddy.implementation.bytecode.Throw;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.web.core.BaseService;
import org.dromara.mall.domain.MallMenu;
import org.dromara.mall.mapper.MallMenuMapper;
import org.dromara.wms.domain.WmsProduct;
import org.dromara.wms.mapper.WmsProductMapper;
import org.dromara.wms.service.WmsProductServiceImpl;
import org.dromara.wms.service.WmsStockService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商城菜单Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class MallMenuService extends BaseService {

    private final MallMenuMapper baseMapper;

    /**
     * 查询商城菜单
     */
    public MallMenu queryById(Long id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询商城菜单列表
     */
    public List<MallMenu> queryListUrl(MallMenu bo) {
      return baseMapper.queryListUrl(bo);
    }

    /**
     * 查询商城菜单列表
     */
    public List<MallMenu> queryList(MallMenu bo) {
        LambdaQueryWrapper<MallMenu> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<MallMenu> buildQueryWrapper(MallMenu bo) {
        LambdaQueryWrapper<MallMenu> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), MallMenu::getName, bo.getName());
        lqw.eq(bo.getStatus() != null, MallMenu::getStatus, bo.getStatus());
        lqw.eq(bo.getParentId() != null, MallMenu::getParentId, bo.getParentId());
        return lqw;
    }

    /**
     * 新增
     */
    public R<Void> insertByBo(MallMenu bo) {
      baseMapper.insert(bo);
      return R.ok("新增成功");
    }

    /**
     * 修改
     */
    public Boolean updateByBo(MallMenu bo) {
        return baseMapper.updateById(bo) > 0;
    }

    /**
     * 批量删除
     */
    public Boolean deleteWithValidByIds(Long[] ids) {
      return baseMapper.deleteBatchIds(List.of(ids)) > 0;
    }

    /* pc端商城 */

  /**
   * 获取全部类别及商品
   * */
  public List<MallMenu> menuList(){
    List<MallMenu> resList = new ArrayList<>();
    MallMenu bo = new MallMenu();
    LambdaQueryWrapper<MallMenu> lqw = buildQueryWrapper(bo);
    List<MallMenu> allMallMenu = baseMapper.selectList(lqw);
    List<WmsProduct> allGoods = this.newGoodList();
    // 1. 将菜单按ID映射到一个Map中
    Map<Long, MallMenu> menuMap = allMallMenu.stream()
      .collect(Collectors.toMap(MallMenu::getId, menu -> menu));
    // 2. 处理每个菜单的子菜单和商品
    for (MallMenu menu : allMallMenu) {
      menu.setChildren(new ArrayList<>()); // 初始化子菜单列表
      menu.setGoods(new ArrayList<>()); // 初始化商品列表
    }
    // 3. 关联商品到对应的菜单
    for (WmsProduct product : allGoods) {
      MallMenu menu = menuMap.get(product.getMallMenuId());
      if (menu != null) {
        menu.getGoods().add(product);
      }
    }
    // 4. 生成菜单的子菜单结构
    for (MallMenu menu : allMallMenu) {
      if (menu.getParentId() != 0) {
        MallMenu parentMenu = menuMap.get(menu.getParentId());
        if (parentMenu != null) {
          parentMenu.getChildren().add(menu);
        }
      }
    }
    // 5. 输出顶级菜单
    List<MallMenu> topLevelMenus = allMallMenu.stream()
      .filter(menu -> menu.getParentId() == 0)
      .collect(Collectors.toList());
    return topLevelMenus;
  }

  private MallMenu mallMenu(MallMenu bo){
    //当前主菜单信息
    MallMenu mallMenu = this.queryById(bo.getId());
    if (mallMenu == null){
      throw new WmsStockService.emptyException("菜单 空数据异常！");
    }
    return mallMenu;
  }

  private List<MallMenu> mallMenuList(MallMenu bo){
    //子菜单信息
    List<MallMenu> mallMenuList = baseMapper.queryListUrl(bo);
    if (mallMenuList.isEmpty()){
      throw new WmsStockService.emptyException("子菜单 空数据异常！");
    }
    return mallMenuList;
  }

  public MallMenu navList(MallMenu bo) {
    //当前主菜单信息
    MallMenu mallMenu = this.mallMenu(bo);
    //子菜单信息
    List<MallMenu> mallMenuList = this.mallMenuList(bo);
    //子菜单下产品信息
    Map<Long, List<WmsProduct>> groupedByMallMenuId = menuProductList(bo);
    for (MallMenu menu : mallMenuList) {
      List<WmsProduct> products = groupedByMallMenuId.get(menu.getId());
      if (products != null) {
        menu.setGoods(products);
      }
    }
    mallMenu.setChildren(mallMenuList);
    return mallMenu;
  }

  private Map<Long, List<WmsProduct>> menuProductList(MallMenu bo){
    List<WmsProduct> wmsProductList = wmsProductList(bo);
    Map<Long, List<WmsProduct>> groupedByMallMenuId = wmsProductList.stream()
      .collect(Collectors.groupingBy(WmsProduct::getMallMenuId));
    return groupedByMallMenuId;
  }

  //获取对应菜单下商品
  private List<WmsProduct> wmsProductList(MallMenu bo){
    List<WmsProduct> wmsProductList = this.queryMenuList(bo);
    return wmsProductList;
  }

  /**
   * 查询产品管理列表&图片
   */
  public List<WmsProduct> queryMenuList(MallMenu bo) {
    return baseMapper.queryProductList(bo);
  }

  /**
   * 查询所有产品
   */
  public List<WmsProduct> newGoodList() {
    MallMenu bo = new MallMenu();
    return baseMapper.queryProductList(bo);
  }
}
